#!/usr/bin/python3
import os
from .Entity import Entity


class Mapper_XML:
    """
    数据库SQL
    """

    def __init__(self, context, entity: Entity) -> None:
        self.context = context
        self.entity: Entity = entity
        self.prefix: str = self.entity.table_name.split("_")[0].lower()

    def code(self):
        package = self.entity.package
        entity_name = self.entity.name
        prefix = self.prefix
        return f"""<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="{package}.{prefix}.mapper.{entity_name}Mapper">

</mapper>
"""

    def save_file(self, dir):
        _target_dir = os.path.join(dir, "1", "mapper", self.prefix)
        if not os.path.exists(_target_dir):
            os.makedirs(_target_dir)
        with open(
            os.path.join(_target_dir, "{}Mapper.xml".format(self.entity.name)),
            mode="w",
            encoding="utf-8",
        ) as f:
            f.write(self.code())
